package com.hison.tandainbao.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hison.tandainbao.entiy.pojo.domain.TbShopInfo;
import com.hison.tandainbao.response.ResponseResult;
import com.hison.tandainbao.service.TbShopInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName: ShopInfoController
 * @Descripiton: 店铺相关控制类
 * @Author: 小郑
 * @Date： 2024/4/1 21:14
 * @Version：1.8
 **/
@RestController
@RequestMapping("/shop")
@Api(value = "店铺相关控制类", tags = "店铺相关控制类")
public class ShopInfoController {

    @Autowired
    private TbShopInfoService service;

    /**
     * 根据id查询商铺信息
     *
     * @param id 商铺id
     * @return 商铺详情数据
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据ID查询店铺信息", produces = "application/json", httpMethod = "GET")
    public ResponseResult queryShopById(@PathVariable("id") Long id) {
        return service.queryById(id);
    }


    /**
     * 新增商铺信息
     *
     * @param shop 商铺数据
     * @return 商铺id
     */
    @PostMapping
    @ApiOperation(value = "新增店铺", produces = "application/json", httpMethod = "POST")
    public ResponseResult saveShop(@RequestBody TbShopInfo shop) {
        return service.saveShopInfo(shop);
    }


    /**
     * 更新商铺信息
     *
     * @param shop 商铺数据
     * @return 无
     */
    @PutMapping
    @ApiOperation(value = "更新店铺信息", produces = "application/json", httpMethod = "PUT")
    public ResponseResult updateShop(@RequestBody TbShopInfo shop) {
        boolean b = service.updateById(shop);
        if (!b) {
            return ResponseResult.error("更新失败");
        }
        return ResponseResult.ok("更新成功");
    }

    /**
     * 根据商铺类型分页查询商铺信息
     *
     * @param typeId  商铺类型
     * @param current 页码
     * @return 商铺列表
     */
    @GetMapping("/of/type")
    public ResponseResult queryShopByType(@RequestParam("typeId") Integer typeId, @RequestParam(value = "current", defaultValue = "1") Integer current, @RequestParam(value = "x", required = false) Double x, @RequestParam(value = "y", required = false) Double y) {
        return service.queryShopByType(typeId, current, x, y);
    }


    /**
     * 根据商铺名称关键字分页查询商铺信息
     *
     * @param name    商铺名称关键字
     * @param current 页码
     * @return 商铺列表
     */
    @GetMapping("/of/name")
    @ApiOperation(value = "根据店铺名称获取店铺信息", produces = "application/json", httpMethod = "GET")
    public ResponseResult queryShopByName(@RequestParam(value = "name", required = false) String name, @RequestParam(value = "current", defaultValue = "1") Integer current) {
        // 根据类型分页查询
        Page<TbShopInfo> page = service.query()
                .like(StrUtil.isNotBlank(name), "name", name)
                .page(new Page<>(current, 10));
        // 返回数据
        return ResponseResult.ok(page.getRecords());
    }

    @GetMapping("/getShopInfo")
    @ApiOperation(value = "获取店铺信息，根据分数排序", produces = "application/json", httpMethod = "GET")
    public ResponseResult getShopInfo(@RequestParam("current") Integer current) {
        return service.getShopInfo(current);
    }


    //----------------------------- 1管理员审核店铺  -----------------------------------
    //审核店铺
    @PutMapping("/adminUpdateStatus")
    @ApiOperation(value = "管理员审核店铺",produces = "application/json",httpMethod = "PUT")
    public  ResponseResult adminUpdateStatus(@RequestParam("id") Integer id,@RequestParam("status") Integer status){
        return  service.adminUpdateStatus(id,status);
    }
    //删除店铺
    @DeleteMapping("/adminDelete/{shopId}")
    @ApiOperation(value = "管理员删除店铺",produces = "application/json",httpMethod = "DELETE")
    public  ResponseResult  delete(@ApiParam("id")  @PathVariable("shopId") String shopId){
        return  service.delete(shopId);
    }


    //管理员获取所有已经发布的店铺
    @GetMapping("/getAllByAdmin")
    @ApiOperation(value = "管理员获取所有已经发布的店铺",produces = "application/json",httpMethod = "GET")
    public ResponseResult getAllByAdmin(@RequestParam(value = "current", defaultValue = "1") Integer current, @RequestParam(value = "total",defaultValue = "10") Integer total){
        return  service.getAllByAdmin(current, total);
    }
}